import requests
import json
import csv

class SeedSpider:
    def __init__(self):
        self.url = 'http://www.seedchina.com.cn/MyAssociation/GetUserListPage'
        self.headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.13 Safari/537.36' }
        self.params = { 'PageSize': self.get_num() }
        self.data_json_path = 'data/data.json'
        self.data_csv_path = 'data/data.csv'

    def get_num(self):
        response = requests.get(
            url = self.url,
            headers = self.headers
        )
        data_json = json.loads(response.text)
        return data_json['Total']
        
    def get_data(self):
        response = requests.get(
            url = self.url,
            headers = self.headers,
            params = self.params
        )
        data_json = json.loads(response.text)
        
        for item in data_json['News']:
            if item['Type'] == '3':
                item['Type'] = '个人'
            else:
                item['Type'] = '单位'
        
        return data_json

    def to_json(self, data):
        with open(self.data_json_path, 'w', encoding = 'utf-8') as f:
            json.dump(data, f, ensure_ascii = False)
            f.close()

    def to_csv(self, data):
        with open(self.data_csv_path, 'w', encoding = 'utf_8_sig') as f:
            csv_writer = csv.writer(f)
            csv_writer.writerow(['序号', '姓名', '会员类型', '职务', '单位名称', '地址', '联系方式'])
            
            for i, item in enumerate(data['News']):
                areaName = item['AreaFullName'] if item['AreaFullName'] else ''
                address = item['Address'] if item['Address'] else ''
                fullAdress = areaName + address

                csv_writer.writerow([
                    i + 1, item['Applicant'], item['Type'], item['DutyName'],
                    item['FirmName'], fullAdress, item['LinkmanMobile'],
                ])

            f.close()
            

if __name__ == '__main__':
    spider = SeedSpider()
    data = spider.get_data()
    spider.to_json(data)
    spider.to_csv(data)